package com.tchy.cloud.service.impl;

import com.tchy.cloud.entity.AAuthEntity;
import com.tchy.cloud.entity.AConsultantEntity;
import com.tchy.cloud.entity.AConsultationInfoEntity;
import com.tchy.cloud.entity.ACustomerEntity;
import com.tchy.cloud.exception.SysAccountExistException;
import com.tchy.cloud.service.AConsultantService;
import com.tchy.cloud.tools.MD5;
import com.tchy.cloud.tools.NeteaseUtil;
import org.jeecgframework.core.common.dao.impl.CommonDao;
import org.jeecgframework.core.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by xiaoji on 2017/2/23.
 */
@Service
@Transactional
public class AConsultantServiceImpl implements AConsultantService {

    @Autowired
    private CommonDao commonDao;
    /**
     * 保存咨询师（创建对应的用户对象、咨询师服务对象、认证图片对象）
     *
     * @param aConsultantEntity
     * @param request
     */
    @Override
    public void saveAConsultant(AConsultantEntity aConsultantEntity, HttpServletRequest request) throws RuntimeException {
        //验证手机号码是否存在
        String phone = aConsultantEntity.getaCustomerEntity().getPhone();
        List<ACustomerEntity> aCustomerEntities = commonDao.findByProperty(ACustomerEntity.class,"phone",phone);
        if(aCustomerEntities.size() > 0){
            throw new SysAccountExistException("手机号码被占用");
        }
        ACustomerEntity aCustomerEntity = aConsultantEntity.getaCustomerEntity();
        String pwd = MD5.getMd5String(aCustomerEntity.getPwd());
        aCustomerEntity.setPwd(pwd);
        aCustomerEntity.setStatus(0);
        //添加用户
        commonDao.save(aCustomerEntity);
        //添加咨询师
        aConsultantEntity.setConsultantId(aCustomerEntity.getCustomerId());
        aConsultantEntity.setCreateTime(new Date());
        aConsultantEntity.setIsExpert(0);
        aConsultantEntity.setIsRecomm(0);
        aConsultantEntity.setSatisfiedNumber(0);
        aConsultantEntity.setOkSatisfied(0);
        aConsultantEntity.setNoSatisfied(0);
        aConsultantEntity.setAppointmentNumber(0);
        aConsultantEntity.setConsultationNum(0);
        aConsultantEntity.setIsAuth(0);
        aConsultantEntity.setLastTime(30);
        aConsultantEntity.setSendNum(0);
        commonDao.save(aConsultantEntity);
        //添加认证图片
        addAuth(request,null);
        //添加服务
        saveAConsultationInfoEntity(aCustomerEntity.getCustomerId(),1,"图文咨询");
        saveAConsultationInfoEntity(aCustomerEntity.getCustomerId(),2,"语音咨询");
        saveAConsultationInfoEntity(aCustomerEntity.getCustomerId(),3,"视频咨询");
        saveAConsultationInfoEntity(aCustomerEntity.getCustomerId(),4,"面对面咨询");

        //创建云信accid
        UUID uuid = UUID.randomUUID();
        String accid = uuid.toString().replaceAll("-","");
        boolean boo = NeteaseUtil.createNeteaseUser(accid, "xxw_" + aCustomerEntity.getCustomerId());
        System.out.println("customer_id = " + aCustomerEntity.getCustomerId() + ";执行状态:" + boo);
        if(boo){
            String upSQL = "update a_customer set accid = ? where customer_id = ? ";
            commonDao.executeSql(upSQL,new Object[]{accid,aCustomerEntity.getCustomerId()});
        }else{
            System.out.println("创建云信accid失败,用户ID=" + aCustomerEntity.getCustomerId());
            throw new SysAccountExistException("创建云信accid失败,用户ID=" + aCustomerEntity.getCustomerId());
        }
    }

    /**
     * 添加咨询师信息
     * @param userId 咨询师id
     * @param type 1图文2电话3视频4面对面
     * @param name 服务名称
     */
    public void saveAConsultationInfoEntity(Long userId,Integer type,String name){
        AConsultationInfoEntity aConsultationInfoEntity = new AConsultationInfoEntity();
        aConsultationInfoEntity.setConsultationId(userId);
        aConsultationInfoEntity.setIsOpen(0);
        aConsultationInfoEntity.setCreateTime(new Date());
        aConsultationInfoEntity.setName(name);
        aConsultationInfoEntity.setPrice(new BigDecimal(0));
        aConsultationInfoEntity.setType(type);
        commonDao.save(aConsultationInfoEntity);
    }

    /**
     * 添加认证图片
     * @param request 请求对象
     * @param consultantId 咨询师地址
     */
    @Override
    public void addAuth(HttpServletRequest request,Long consultantId){
        String auth1=request.getParameter("auth1");
        String auth2=request.getParameter("auth2");
        String auth3=request.getParameter("auth3");
        String auth4=request.getParameter("auth4");
        String auth5=request.getParameter("auth5");
        if(consultantId != null){
            String sql="delete from a_auth where consultant_id=?";
            commonDao.executeSql(sql,new Object[]{consultantId});
        }
        addSingleAuth(auth1,consultantId);
        addSingleAuth(auth2,consultantId);
        addSingleAuth(auth3,consultantId);
        addSingleAuth(auth4,consultantId);
        addSingleAuth(auth5,consultantId);
    }

    /**
     * 添加单个图片
     * @param path
     */
    public void addSingleAuth(String path,Long consultantId){
        if(!StringUtil.isEmpty(path)){
            AAuthEntity aAuthEntity = new AAuthEntity();
            aAuthEntity.setConsultantId(consultantId);
            aAuthEntity.setImage(path);
            aAuthEntity.setUploadTime(new Date());
            commonDao.save(aAuthEntity);
        }
    }
}
